package com.buba.customer.controller;

import com.buba.customer.pojo.CustomerLevelInf;
import com.buba.customer.service.CustomerLevelInfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员级别控制层
 */
@Controller
@RequestMapping("/CustomerLevelInf")
public class CustomerLevelInfController {
    private CustomerLevelInfService customerLevelInfService;


    @RequestMapping("/addCustomerLevelInf.do")
    @ResponseBody
    public Map<String, Object> addCustomerLevelInf(String[] level_name, String[] min_point, String[] max_point) {
        Map<String, Object> map = new HashMap<>();
        CustomerLevelInf customerLevelInf;
        List<CustomerLevelInf> list = new ArrayList<>();
        for (int i = 0; i < level_name.length; i++) {
            customerLevelInf = new CustomerLevelInf();
            customerLevelInf.setLevel_name(level_name[i]);
            customerLevelInf.setMin_point(Integer.valueOf(min_point[i]));
            customerLevelInf.setMax_point(Integer.valueOf(max_point[i]));
            list.add(customerLevelInf);
        }

        // 添加前把数据库中所有会员级别的数据删除
        //执行删除会员级别
        customerLevelInfService.delCustomerLevelInf();
        //把新的会员级别添加到表中
        Integer count = customerLevelInfService.addCustomerLevelInf(list);
        map.put("msg","存入了"+count+"条数据");

        return map;
    }


    @RequestMapping("/selCustomerLevelInf.do")
    @ResponseBody
    public Map<String, Object> selCustomerLevelInf() {
        Map<String, Object> map = new HashMap<>();
        List<CustomerLevelInf> list = customerLevelInfService.selCustomerLevelInf();
        map.put("data", list);
        return map;
    }




    @Autowired
    public void setCustomerLevelInfService(CustomerLevelInfService customerLevelInfService) {
        this.customerLevelInfService = customerLevelInfService;
    }
}
